//
//  ViewController.swift
//  Pedometer
//
//  Created by Pengwei on 2019/5/27.
//  Copyright © 2019 Pengwei. All rights reserved.
//

import UIKit
import PedometerLib
class ViewController: UIViewController {

    @IBOutlet weak var stepLabel: UILabel!
    let pstep = PedometerLib()
    
    // 服务器
    let httpUrl = "http://101.132.71.126:8088/api/exchange"
    // 请求头
    let headData = ["appId": "3252191199100",
                    "sigParam":  "4e369e9fac857e414a9e7f3324a5c913",
                    "auth": "MzI1MjE5MTE5OTEwMC0yMDE5MDUxOA=="]
    // 用户 id
    let userId = "00008568857213_APP"
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }

    @IBAction func getStepFromHealth() {
        pstep.getStepCount(){(count:String?, error: Error?) in
            print("错误=", error as Any)
            if error == nil {
                DispatchQueue.main.async {
                    self.stepLabel.text = count
                }
            }else{
                print("执行错误")
            }
        }
    }
    
    @IBAction func stepToServer() {
        pstep.stepCount2Server(Url: httpUrl, Headers: headData, Uid: userId){ (res, error) in
            print("请求返回数据==", res as Any , error as Any)
            if error == nil {
                DispatchQueue.main.async {
                    self.stepLabel.text = "上传成功"
                }
                
                print("上传成功=")
            }else{
                DispatchQueue.main.async {
                     self.stepLabel.text = "上传失败"
                }
                print("上传失败=", error as Any)
            }
        }
    }
    
}

